package org.apache.camel.component.coherence;

import static org.apache.camel.component.coherence.CoherenceConstants.*;

/**
 * Class containing the coherence cluster configuration
 * parameters exposed to the component user.
 * 
 * @author gheema
 */
@SuppressWarnings("serial")
public class CoherenceClusterConfiguration implements java.io.Serializable {

	private String clusterName;
	private String clusterMulticastAddress;
	private String clusterPort;
	private String clusterTTL;
	
	public CoherenceClusterConfiguration(String clusterName,
			String clusterMulticastAddress,String clusterPort, String clusterTTL) {
		super();
		this.validateClusterName(clusterName);
		this.clusterName = clusterName;
		this.clusterMulticastAddress = clusterMulticastAddress;
		this.clusterPort = clusterPort;
		this.clusterTTL = clusterTTL;
	}
	
	private void validateClusterName(String clusterName) {
		if(clusterName == null || 
				clusterName.isEmpty() || 
				clusterName.length() > 30) {
			
			throw new IllegalArgumentException("The given Oracle Coherence " +
					"cluster name:<"+clusterName+"> must not be null, not empty, " +
							"and not longer than 30 char");
		}
	}
	
	/**
	 * Sets the given properties as sytem properties
	 */
	protected void applyClusterConfiguration() {
		System.setProperty(COHERENCE_CONFIG_CLUSTER_ADDRESS_PARAM.getValue(), clusterMulticastAddress);
		System.setProperty(COHERENCE_CONFIG_CLUSTER_PORT_PARAM.getValue(), clusterPort);
		System.setProperty(COHERENCE_CONFIG_CLUSTER_NAME_PARAM.getValue(), clusterName);
		System.setProperty(COHERENCE_CONFIG_CLUSTER_TTL_PARAM.getValue(), clusterTTL);
	}
	
	
	public String getClusterName() {
		return clusterName;
	}

	public void setClusterName(String clusterName) {
		this.clusterName = clusterName;
	}

	public String getClusterMulticastAddress() {
		return clusterMulticastAddress;
	}

	public void setClusterMulticastAddress(String clusterMulticastAddress) {
		this.clusterMulticastAddress = clusterMulticastAddress;
	}

	public String getClusterPort() {
		return clusterPort;
	}

	public void setClusterPort(String clusterPort) {
		this.clusterPort = clusterPort;
	}

	@Override
	public String toString() {
		return "CoherenceClusterConfiguration [clusterName=" + clusterName
				+ ", clusterMulticastAddress=" + clusterMulticastAddress
				+ ", clusterPort=" + clusterPort + ", clusterTTL=" + clusterTTL
				+ "]";
	}
	
}
